package com.njkwt.business.system.controller;

import com.njkwt.business.system.dto.OrgDto;
import com.njkwt.business.system.dto.PageDto;
import com.njkwt.business.system.dto.ResponseDto;
import com.njkwt.business.system.service.OrgService;
import com.njkwt.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
* @ClassName OrgController
* @Description
* @Author ydj (171799383@qq.com)
* @Date  2020-8-18 12:38:41
* @Version V1.0
**/

@RestController
@RequestMapping("/api/business/system/org")
public class OrgController {

    private static final Logger LOG = LoggerFactory.getLogger(OrgController.class);
    public static final String BUSINESS_NAME = "组织表";

    @Resource
    private OrgService orgService;

    /**
     * 列表查询
     */
    @PostMapping("/list")
    public ResponseDto list(@RequestBody PageDto pageDto) {
        ResponseDto responseDto = new ResponseDto();
        orgService.list(pageDto);
        responseDto.setContent(pageDto);
        return responseDto;
    }

    /**
     * 保存，id有值时更新，无值时新增
     */
    @PostMapping("/save")
    public ResponseDto save(@RequestBody OrgDto orgDto) {
        // 保存校验
        ValidatorUtil.require(orgDto.getId(), "流水号");
        ValidatorUtil.length(orgDto.getId(), "流水号", 1, 50);
        ValidatorUtil.require(orgDto.getCascadeid(), "节点语义ID");
        ValidatorUtil.length(orgDto.getCascadeid(), "节点语义ID", 1, 255);
        ValidatorUtil.require(orgDto.getName(), "组织名称");
        ValidatorUtil.length(orgDto.getName(), "组织名称", 1, 255);
        ValidatorUtil.require(orgDto.getHotkey(), "热键");
        ValidatorUtil.length(orgDto.getHotkey(), "热键", 1, 255);
        ValidatorUtil.require(orgDto.getParentname(), "父节点名称");
        ValidatorUtil.length(orgDto.getParentname(), "父节点名称", 1, 255);
        ValidatorUtil.require(orgDto.getIsleaf(), "是否叶子节点");
        ValidatorUtil.require(orgDto.getIsautoexpand(), "是否自动展开");
        ValidatorUtil.require(orgDto.getIconname(), "节点图标文件名称");
        ValidatorUtil.length(orgDto.getIconname(), "节点图标文件名称", 1, 255);
        ValidatorUtil.require(orgDto.getStatus(), "当前状态");
        ValidatorUtil.require(orgDto.getBizcode(), "业务对照码");
        ValidatorUtil.length(orgDto.getBizcode(), "业务对照码", 1, 255);
        ValidatorUtil.require(orgDto.getCustomcode(), "自定义扩展码");
        ValidatorUtil.require(orgDto.getCreatetime(), "创建时间");
        ValidatorUtil.require(orgDto.getCreateid(), "创建人ID");
        ValidatorUtil.require(orgDto.getSortno(), "排序号");
        ValidatorUtil.length(orgDto.getParentid(), "父节点流水号", 1, 50);
        ValidatorUtil.length(orgDto.getTypename(), "分类名称", 1, 20);
        ValidatorUtil.length(orgDto.getTypeid(), "分类ID", 1, 50);

        ResponseDto responseDto = new ResponseDto();
        orgService.save(orgDto);
        responseDto.setContent(orgDto);
        return responseDto;
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto delete(@PathVariable String id) {
        ResponseDto responseDto = new ResponseDto();
        orgService.delete(id);
        return responseDto;
    }
}
